package com.lagou.homework_1_4;
//编写一个员工类
public class Staff {

    //创建成员变量 姓名和年龄
    private String name;
    private int age;

    //创建无参构造
    public Staff() {
    }

    //创建有参构造并抛出异常
    public Staff(String name, int age) throws AgeException {
        setName(name);
        setAge(age);
    }

    //创建 get，set方法
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
         return age;
    }

    //重写toString方法
    @Override
    public String toString() {
        return "Staff{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    //异常值判断及抛出异常
    public void setAge(int age) throws AgeException {
        if (age>18 && age<150){
            this.age = age;
        }else {
            //异常类的使用
            throw new AgeException("年龄不再合理范围！");
        }


    }
}
